package com.ruoyi.system.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class WxCode {
    private static WxCode instance;
    public static WxCode getInstance() {
        if (instance == null) {
            instance = new WxCode();
        }
        return instance;
    }

    public String getAccessToken(String appid) {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>();
        String appsecret = AppCode.getValueByKey(appid);
        params.put("APPID", appid);
        params.put("APPSECRET", appsecret);
        ResponseEntity<String> reponseEntity = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
        String body = reponseEntity.getBody();
        JSONObject object = JSON.parseObject(body);
        String Access_Token = object.getString("access_token");
        return Access_Token;
    }
}
